package Integrity;

import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.xml.sax.SAXException;

/**
 * @author WabeyaB
 *
 */
public class Xml_Extracted_Data {
	/**
	 * @throws Exception 
	 * 
	 */
	public Xml_Extracted_Data(String filename)  throws ParserConfigurationException, SAXException, IOException, XPathExpressionException, Exception {
	
		DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance();
		builder = dbfactory.newDocumentBuilder();
		XPathFactory xpfactory = XPathFactory.newInstance();
		path = xpfactory.newXPath();
		f = new File(filename);
		doc = builder.parse(f);
		
		scraped_error_code = path.evaluate("/scrape-session/error-code",doc);		
		scraped_base_url = path.evaluate("/scrape-session/base-url",doc);
		scraped_type = path.evaluate("/scrape-session/type",doc);
		scraped_date = path.evaluate("/scrape-session/date",doc);
		scraped_time = path.evaluate("/scrape-session/time",doc);
		String date_time = scraped_date + " " + scraped_time;
		Derived_date_with_time = stringToDate(date_time);
		
		if (scraped_error_code.substring(0,3).equals("ERR")) {
			notdone = false;
		}
		
		if (notdone) {
			
			scraped_statm_no = path.evaluate("/scrape-session/statement-number",doc);
			scraped_statm_month = path.evaluate("/scrape-session/statement-month",doc);
			scraped_acc_no = path.evaluate("/scrape-session/datapair[1]/value",doc);
			scraped_acc_holder = path.evaluate("/scrape-session/datapair[2]/value",doc);
			scraped_t_due = getDoubleValue(path.evaluate("/scrape-session/datapair[3]/value",doc));
			scraped_due_date = path.evaluate("/scrape-session/datapair[4]/value",doc);
			scraped_op_bal = getDoubleValue(path.evaluate("/scrape-session/datapair[5]/value",doc));
			scraped_cl_bal = getDoubleValue(path.evaluate("/scrape-session/datapair[6]/value",doc));
			scraped_pay_rec = getDoubleValue(path.evaluate("/scrape-session/datapair[7]/value",doc));
			scraped_new_chrg = getDoubleValue(path.evaluate("/scrape-session/datapair[8]/value",doc));
			scraped_ded = getDoubleValue(path.evaluate("/scrape-session/datapair[9]/value",doc));
			scraped_disc = getDoubleValue(path.evaluate("/scrape-session/datapair[10]/value",doc));
			scraped_vat_amount = getDoubleValue(path.evaluate("/scrape-session/datapair[11]/value",doc));
			
			if (scraped_type.equals("telco")){
				scraped_tel_no = path.evaluate("/scrape-session/datapair[12]/value",doc);
				scraped_serv_chrg = getDoubleValue(path.evaluate("/scrape-session/datapair[13]/value",doc));
				scraped_call_chrg = getDoubleValue(path.evaluate("/scrape-session/datapair[14]/value",doc));
				scraped_t_no_call = getIntegerValue(path.evaluate("/scrape-session/datapair[15]/value",doc));
				scraped_t_call_dur = path.evaluate("/scrape-session/datapair[16]/value",doc);
			}
			else if (scraped_type.equals("credit")){
				scraped_card_type = path.evaluate("/scrape-session/datapair[12]/value",doc);
				scraped_int_rate = getDoubleValue(" "+ path.evaluate("/scrape-session/datapair[13]/value",doc));
				scraped_credit_lim = getDoubleValue(path.evaluate("/scrape-session/datapair[14]/value",doc));
				scraped_credit_avl = getDoubleValue(path.evaluate("/scrape-session/datapair[15]/value",doc));
				scraped_min_amount_due = getDoubleValue(path.evaluate("/scrape-session/datapair[16]/value",doc));
			}
			else if (scraped_type.equals("municipality")){
				scraped_elec_used = path.evaluate("/scrape-session/datapair[12]/value",doc);
				scraped_elec_chrg = getDoubleValue(path.evaluate("/scrape-session/datapair[13]/value",doc));
				scraped_water_used = path.evaluate("/scrape-session/datapair[14]/value",doc);
				scraped_water_chrg = getDoubleValue(path.evaluate("/scrape-session/datapair[15]/value",doc));
				scraped_sew_chrg = getDoubleValue(path.evaluate("/scrape-session/datapair[16]/value",doc));
				scraped_ref_chrg = getDoubleValue(path.evaluate("/scrape-session/datapair[17]/value",doc));
			}
			else {
				throw new Exception ("Unknown Service Provider Type");
			}
		}
	}
	public double getDoubleValue(String str){
		int str_length = str.length();
		return Double.parseDouble(str.substring(1,str_length));
	}
	
	public int getIntegerValue(String str){
		int str_length = str.length();
		return Integer.parseInt(str.substring(0,str_length));		
	}
	public Date stringToDate (String str) throws ParseException{
		Date dt = df.parse(str);
		return dt;
	}
	
	
	public String get_scraped_error_code() {
		return scraped_error_code;
	}
	public String get_scraped_base_url(){
		return scraped_base_url;
	}
	public String get_scraped_type() {
		return scraped_type;
	}
	public Date get_scraped_date_with_time() {
		return Derived_date_with_time;
	}
	public String get_scraped_time() {
		return scraped_time;
	}
	
	public String get_scraped_statm_no() {
		return scraped_statm_no;
	}
	public String get_scraped_statm_month() {
		return scraped_statm_month;
	}
	public String get_scraped_acc_no() {
		return scraped_acc_no;
	}
	public String get_scraped_acc_holder() {
		return scraped_acc_holder;
	}
	
	public String get_scraped_due_date(){
		return scraped_due_date;
	}
	public double get_scraped_new_chrg(){
		return scraped_new_chrg;
	}
	public double get_scraped_pay_rec(){
		return scraped_pay_rec;
	}
	public double get_scraped_op_bal(){
		return scraped_op_bal;
	}
	public String get_scraped_t_call_dur(){
		return scraped_t_call_dur;
	}
	public double get_scraped_vat_amount(){
		return scraped_vat_amount;
	}
	public double get_scraped_call_chrg(){
		return scraped_call_chrg;
	}
	public double get_scraped_serv_chrg(){
		return scraped_serv_chrg;
	}
	public double get_scraped_ded(){
		return scraped_ded;
	}
	public double get_scraped_disc(){
		return scraped_disc;
	}
	public double get_scraped_t_due(){
		return scraped_t_due;
	}
	public double get_scraped_cl_bal(){
		return scraped_cl_bal;
	}
	public String get_scraped_tel_no() {
		return scraped_tel_no;
	}
	public int get_scraped_t_no_call() {
		return scraped_t_no_call;
	}
	
	public String get_scraped_card_type() {
		return scraped_card_type;
	}
	public double get_scraped_int_rate() {
		return scraped_int_rate;
	}
	public double get_scraped_credit_lim() {
		return scraped_credit_lim;
	}
	public double get_scraped_credit_avl() {
		return scraped_credit_avl;
	}
	public double get_scraped_min_amount_due() {
		return scraped_min_amount_due;
	}
	
	public String get_scraped_elec_used() {
		return scraped_elec_used;
	}
	public double get_scraped_elec_chrg() {
		return scraped_elec_chrg;
	}
	public String get_scraped_water_used() {
		return scraped_water_used;
	}
	public double get_scraped_water_chrg() {
		return scraped_water_chrg;
	}
	public double get_scraped_sew_chrg() {
		return scraped_sew_chrg;
	}
	public double get_scraped_ref_chrg() {
		return scraped_ref_chrg;
	}
	
	private Boolean notdone = true;
	private DocumentBuilder builder;
	private XPath path;
	private File f;
	Document doc;
	DateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
	
	private String scraped_error_code ="";	
	private String scraped_base_url="";
	private String scraped_type="";
	private Date Derived_date_with_time;
	private String scraped_date;
	private String scraped_time="";
	
	private String scraped_statm_no ="";
	private String scraped_statm_month = "";
	private String scraped_acc_no="";
	private String scraped_acc_holder="";
	private double scraped_t_due;
	private String scraped_due_date="";
	private double scraped_op_bal;
	private double scraped_cl_bal;
	private double scraped_pay_rec;
	private double scraped_new_chrg;
	private double scraped_ded;
	private double scraped_disc;
	private double scraped_vat_amount;
	
	private String scraped_tel_no="";
	private double scraped_serv_chrg;
	private double scraped_call_chrg;
	private int scraped_t_no_call;
	private String scraped_t_call_dur="";
	
	private String scraped_card_type="";
	private double scraped_int_rate;
	private double scraped_credit_lim;
	private double scraped_credit_avl;
	private double scraped_min_amount_due;
	
	private String scraped_elec_used="";
	private double scraped_elec_chrg;
	private String scraped_water_used="";
	private double scraped_water_chrg;
	private double scraped_sew_chrg;
	private double scraped_ref_chrg;
			
}
